/*
 *  Copyright (C) 2010 takaji
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * TransactionListStep.java
 *
 * Created on Jan 24, 2010, 11:24:00 PM
 */
package dakside.hacc.modules.databridge.dataagent;

import dakside.hacc.core.models.Transaction;
import org.dakside.duck.dakwizard.AbstractWizardStep;
import dakside.hacc.modules.transactions.trxman.TransactionTableModel;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author takaji
 */
public class TransactionListStep extends AbstractWizardStep<ImportLoaderWizardModel> {

    /** Creates new form TransactionListStep */
    public TransactionListStep(ImportLoaderWizardModel model) {
        super(model, true);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        transactionTable1 = new dakside.hacc.modules.transactions.trxman.TransactionTable();

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        transactionTable1.setModel(new TransactionTableModel());
        transactionTable1.setName("transactionTable1"); // NOI18N
        jScrollPane1.setViewportView(transactionTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public boolean isCancellable() {
        return true;
    }

    @Override
    public boolean isFinishable() {
        return true;
    }

    @Override
    public boolean canGoNext() {
        return true;
    }

    @Override
    public boolean canGoBack() {
        return true;
    }

    @Override
    public void onShow() {
        System.out.println("Me show!");
        final ConversionWorker worker = new ConversionWorker(
                getWizardModel().getDataSource(), getWizardModel().getDataDefinition());
        worker.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equalsIgnoreCase("state")
                        && evt.getNewValue().toString().equals("DONE")) {
                    try {
                        Transaction[] trxs = worker.get();
                        getWizardModel().setModelset(trxs);
                        transactionTable1.load(trxs);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(TransactionListStep.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ExecutionException ex) {
                        Logger.getLogger(TransactionListStep.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        worker.execute();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private dakside.hacc.modules.transactions.trxman.TransactionTable transactionTable1;
    // End of variables declaration//GEN-END:variables
}
